package com.ctg.itrdc.sysmgr.portal.control;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.portal.api.dto.PrivGrantDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IPrivGrantService;
import com.ctg.itrdc.sysmgr.portal.common.constant.PrivGrantEnum.GrantSysobjType;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.data.entity.PrivGrant;
import com.ctg.itrdc.sysmgr.portal.exception.SysmgrContrlException;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;

@Controller
@RequestMapping("/priv_grant")
public class PrivGrantController extends SysmgrCURDController<Long, PrivGrantDTO> {
    
	
	@Resource
	private IPrivGrantService privGrantService;
	
	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid PrivGrantDTO dto ,BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		if(dto.getGrantObjType()!=null)
			dto.setGrantObjType(GrantSysobjType.valueOf(dto.getGrantObjType()).toString());
		long i = privGrantService.insert(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功",i);
	}
	
	/**
	 * @api {get} /priv_grant/batch/insert 批量授权
	 * @apiGroup priv_grant
	 * @apiName batchInsert
	 * @apiParam {String} ids 权限id列表，用逗号隔开，如：x,y,z 
	 * @apiParam {String} grantObjType 授权对象类型，区分是系统用户、系统岗位、系统角色
	 * @apiParam {String} operType 授权操作类型，LOVB，区分是：ADD包含、DEL去除
	 * @apiParam {Long} grantObjId 授权对象标识，根据授权对象类型，区分是系统用户ID、系统岗位ID、角色ID
	 * @apiParam {String} statusCd 状态
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/batch/insert")
	@ResponseBody
	public CommonResult batchInsert(
			@RequestParam(required=false) String ids,
			@Valid PrivGrantDTO dto,BindingResult result){
		
		if(StringUtils.isBlank(ids)){
			throw new SysmgrContrlException("权限ids[privIds]不能为空~");
		}
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		
		return new CommonResult(HttpStatus.OK.toString(),"批量新增成功",privGrantService.batchInsert(ids,dto));
	}
	/**
	 * @api {get} /priv_grant/batch/delete/privIds 根据权限id列表批量刪除
	 * @apiGroup priv_grant
	 * @apiName batchDeleteByPrivIds
	 * @apiParam {String} ids 权限id列表，用逗号隔开，如：x,y,z 
	 * @apiParam {String} grantObjType 授权对象类型，区分是系统用户、系统岗位、系统角色
	 * @apiParam {Long} grantObjId 授权对象标识，根据授权对象类型，区分是系统用户ID、系统岗位ID、角色ID
	 * @apiParam {String} statusCd 状态
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/batch/delete/privIds")
	@ResponseBody
	public CommonResult batchDeleteByPrivIds(
			@RequestParam String ids,PrivGrantDTO dto){
		
		privGrantService.batchDeleteByPrivIds(ids,dto);
		return new CommonResult(HttpStatus.OK.toString(),"批量删除成功");
	}
}
